package com.thread.async;

/**
 * @author wangyingjie
 * @date
 * @remark
 */
public class ThreadDemo01 {
//    private static boolean flag = false;

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(){
            @Override
            public void run() {
                while (true){
                    System.out.println("执行线程体了");
                    if (Thread.currentThread().isInterrupted()){//是否中断
                        System.out.println("子线程中断了。。。");
                        break;
                    }
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        System.out.println("调用sleep");
                        //由于如果sleep方法发生异常，致使线程中断，并标志清除，所以需要再次手动设置中断标志
                        Thread.currentThread().interrupt();
                    }
                }
            }
        };
        thread.start();
        Thread.sleep(2000);
        thread.interrupt();//中断子线程
    }
}
